lobsterh5.main({
    data: {
        dept: [],
        layer: [],
        searchkey: [],
        // currentbranch: {},
    },
    pageload() {
        var self = this;
        // self.data.currentbranch = JSON.parse(lobsterh5.GetAppStorage("selectbranch")) || {};
        self.getdata();
        self.initevent();
    },
    initevent() {
        var self = this;
        $(document).on("click", ".deptLi", function (obj) {
            $(this).addClass('active').siblings('li').removeClass('active');
            // var index = $(this).index();
            // $('.j-content').eq(index).show().siblings('.j-content').hide();
            var deptlayer = obj.currentTarget.dataset.info;
            self.showdept(JSON.parse(deptlayer));
        })
        $(document).on("click", ".weui-cell_access", function (obj) {
            var info = JSON.parse(obj.currentTarget.dataset.info);
            location.href = "docsignalsource.html?appid="+lobsterh5.GetAppId()+"&deptId=" + info.DeptId;
        });
        $('#searchForm').on('submit', function (event) {
            //拦截表单默认提交事件
            event.preventDefault();
            //获取input框的值，用ajax提交到后台
            self.data.searchkey = $('#searchInput').val();
            if (self.data.searchkey && self.data.searchkey.length > 0) {
                self.filterdata();
            }
            else {
                $("#content").show();
                $("#nodata").hide();
                self.inittemp("tpl", "sidebar", self.data.layer);
                self.showdept(self.data.layer[0]);
            }

        });
    },
    getdata() {
        var self = this;
        lobsterh5.GETAPI('tsregister.depttree').then(res => {
            $(".weui-loadmore").hide();
            var data = res;
            self.data.dept = data.dept;
            self.data.layer = data.layer;
            self.inittemp("tpl", "sidebar", self.data.layer);
            self.showdept(self.data.layer[0]);
        })
    },
    filterdata() {
        var self = this;
        var layer = [];
        var data = self.data.dept.filter((item) => {
            return item.Name.indexOf(self.data.searchkey) > -1
        });
        data.forEach(item => {
            var layerfilter = self.data.layer.filter(p => p.LayerId == item.Layer);
            var layerArr = layer.filter(p => p.LayerId == layerfilter[0].LayerId);
            if (!layerArr || layerArr.length == 0) {
                layer.push(layerfilter[0]);
            }
        })

        console.log(data);
        console.log(layer);
        if (layer && layer.length > 0) {
            self.showdept2(layer[0]);

            $("#content").show();
            $("#nodata").hide();
            self.inittemp("tpl", "sidebar", layer);
        } else {
            $("#content").hide();
            $("#nodata").show();
        }

    },
    showdept(deptlayer) {
        var self = this;
        var filterdata = self.data.dept.filter((x, index, arr) => {
            return x.Layer == deptlayer.LayerId
        });
        this.inittemp("tpldept", "sidebaritem", filterdata);
    },
    showdept2(deptlayer) {
        var self = this;
        var filterdata = self.data.dept.filter((x, index, arr) => {
            return x.Layer == deptlayer.LayerId && x.Name.indexOf(self.data.searchkey) > -1
        });
        this.inittemp("tpldept", "sidebaritem", filterdata);
    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, { list: data });
        $("#" + _id).html(html);
    },
}, false);